//index.js
//获取应用实例
const wx = require('../../libs/wx.js');
const config = require('../../config');
const Page = require('../../libs/BasePage');
const app = getApp();
const {submitOrder} = require('../../apis');


function showSuccessToast(msg) {
    wx.showToast({
        title: msg,
        image: '../../assets/ic_toast_yes.png'
    })
}

Page({
    data: {
        company: {},
        sales: [],
        clerks: [],
        showWXAdvice: false,
        showFreeAdvice: false,
        showOrderAdvice: false,
        showWX: false,
        canShowWX: false,
        animationData: {},
        orderPhone: null
    },

    onShow() {
        const animation = wx.createAnimation({
            duration: 500,
            timingFunction: 'ease',
        });

        const func = () => {
            animation.translateY(-5).scale(1.5).step();
            this.setData({
                animationData: animation.export()
            });

            animation.translateY(0).scale(1).step();
        };

        func();

        setInterval(func, 1000)
    },

    onShareAppMessage() {
        return {
            title: config.name,
            path: '/pages/index/index',
        }
    },

    goToMap() {
        wx.openLocation({
            longitude: this.data.company.location[0],
            latitude: this.data.company.location[1],
            name: config.name,
            address: this.data.company.address
        });
    },

    goToEnv() {
        wx.navigateTo({
            url: '../env/env'
        })
    },

    goToClerks() {
        wx.navigateTo({
            url: '../clerks/clerks'
        })
    },

    goToSales() {
        wx.navigateTo({
            url: '../sales/sales'
        })
    },

    goToDetail(e) {
        const url = `../clerk/clerk?userId=${e.currentTarget.dataset.userid}&hot=${e.currentTarget.dataset.hot}&role=${e.currentTarget.dataset.role}`;
        wx.navigateTo({
            url
        })
    },

    onCallClick() {
        wx.makePhoneCall({
            phoneNumber: this.data.company.phoneNum || ''
        })
    },


    onWXAdviceClick() {
        if (this.data.canShowWX) {
            this.setData({showWX: true});
        } else {
            this.setData({showWXAdvice: true});
        }
    },

    onWXCancelClick() {
        this.setData({showWX: false});
    },

    onWXAdviceCancelClick() {
        this.setData({showWXAdvice: false});
    },

    onOrderAdviceClick() {
        this.setData({showOrderAdvice: true});
    },

    onOrderAdviceCancelClick() {
        this.setData({showOrderAdvice: false});
    },

    onFreeAdviceCancelClick() {
        this.setData({showFreeAdvice: false});
    },

    onFreeAdviceClick() {
        this.setData({showFreeAdvice: true});
    },

    bindOrderPhone(e) {
        this.setData({
            orderPhone: e.detail.value
        })

    },

    onLoad() {
        wx.showShareMenu({
            withShareTicket: true
        })

        app.getCompanyDetail().then((res) => {

            const {clerks, company, sales} = res;

            this.setData({clerks, company, sales});
        })
    },

    submit(e) {
        if (this.data.orderPhone && this.data.orderPhone.length == 11)
            submitOrder(this.data.orderPhone, e.target.dataset.type).then(res => {
                if (e.target.dataset.type == 1) {
                    this.setData({showWXAdvice: false, canShowWX: true, showWX: true})
                    showSuccessToast('提交成功！');
                } else if (e.target.dataset.type == 2) {
                    this.setData({showOrderAdvice: false})
                    showSuccessToast('提交成功，等待客服与您联系！');
                } else if (e.target.dataset.type == 3) {
                    this.setData({showFreeAdvice: false})
                    showSuccessToast('提交成功，等待客服与您联系！');
                }
            })
    },

    previewImage(e) {
        wx.previewImage({
            current: e.currentTarget.dataset.url,
            urls: e.currentTarget.dataset.urls
        })
    },

    previewSale(e) {
        wx.previewImage({
            current: e.currentTarget.dataset.url,
            urls: e.currentTarget.dataset.urls
        })
    },

    getRoleStr(role) {
        if (role === 2)
            return '主理人';
        if (role === 1)
            return '店长';
        return '纹身师';
    }
});
